class Article < ActiveRecord::Base
  # This plugin is based on acts_as_taggable by DHH but includes extras
  # such as tests, smarter tag assignment, and tag cloud calculations.  
  # http://github.com/jviney/acts_as_taggable_on_steroids
  acts_as_taggable
  
  belongs_to :category
  belongs_to :recipe
  has_many :contents, :order => "position"

  acts_as_list :scope => :recipe
  
  validates_presence_of :title
  
  LANGUAGES = [
    # Displayed           stored in DB    
    ["C",                 "c"],
    ["C++",               "c++"],
    ["CSS",               "css"],
    ["Css Experimental",  "css_experimental"],    
    ["HAML",              "haml"],
    ["HTML for Rails",    "html_rails"],      
    ["Javascript",        "javascript"],
    ["jQuery",            "jquery_javascript"],
    ["Objective-C",       "objective-c"],  
    ["Objective-C++",     "objective-c++"],
    ["Ruby on Rails",     "ruby_on_rails"],  
    ["Ruby",              "ruby"]    
  ]
  
  named_scope :ready_for_sort, :conditions => "recipe_id IS NULL"
    
  def self.all_by_language(language)
    Article.find(:all, :include => "contents", :conditions => ["contents.language_type = ?", language])
  end
  
  def self.count_by_language(language)
    Article.count(:all, :include => "contents", :conditions => ["contents.language_type = ?", language])
  end  
  
  def self.paginate_with_default(params)
    self.paginate :page => params[:page], :per_page => 15, :order => "id DESC"
  end
  
  def self.update_position(ids)
    ids.each do |id|
      Article.find(id).update_attribute(:position, ids.index(id))
    end    
  end
end
